﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WW.NActiveRecord.Internal.Utils
{
    /// <summary>
    /// 代表一个命名参数
    /// </summary>
    internal class NamedParameter {
        private string name;
        private Func<string, object> value;

        //参数值 使用时才获取
        public object Value {
            get { return value(name); }
        }

        //参数名
        public string Name {
            get { return name; }
        }

        //固定值
        public static NamedParameter NewFixedParameter(string name, object value) {
            return new NamedParameter { name = name, value = n => value };
        }

        /// <summary>
        ///  例：
        ///  Func<string, object> valueCallback = firstName=>"chen";
        ///  "firstName"作为参数名,而valueCallback("firstName")作为参数值
        /// 
        ///  可以利用闭包特性延迟参数求值
        /// </summary>
        /// <param name="valueCallback"></param>
        /// <returns></returns>
        public static NamedParameter NewDynamicParameter<TResult>(Func<string, TResult> valueCallback) {
            return new NamedParameter { name = valueCallback.Method.GetParameters()[0].Name, value = n => valueCallback(n) };
        }

        public static NamedParameter NewDynamicParameter<TResult>(string name,Func<TResult> valueCallback) {
            return new NamedParameter { name = name, value = n => valueCallback() };
        }
    }
}